package controllers

import (
	"fmt"
	"log"
	"time"

	"face/facesdk"
	"face/models"
	"github.com/astaxie/beego"
	// "github.com/go-xorm/xorm"
)

func init() {
	fmt.Println("开始运行face")
}

type UserListController struct {
	beego.Controller
}

func (c *UserListController) Get() {
	users := make([]*models.Person, 0, 10)
	models.Engine.Where("attribute = ?", "user").Find(&users)

	c.Data["json"] = map[string]interface{}{"User": users}
	c.ServeJson()
}

type UserCreateController struct {
	beego.Controller
}

func (c *UserCreateController) Get() {

	// 备注
	command := c.GetString("Command")

	// 用户名
	personName := c.GetString("PersonName")
	if personName == "" {
		c.Data["json"] = GetErrorMessage("Required Param PersonName")
		c.ServeJson()
		return
	}

	// 用户识别
	attribute := c.GetString("Attribute")
	if attribute == "" {
		c.Data["json"] = GetErrorMessage("Required Param Attribute")
		c.ServeJson()
		return
	}

	if attribute != "user" && attribute != "stranger" {
		c.Data["json"] = GetErrorMessage("Attribute Error")
		fmt.Println(attribute)
		c.ServeJson()
		return
	}

	user := new(models.Person)
	user.PersonName = personName

	// 用户名必须是全局唯一的
	// check username
	has, _ := models.Engine.Get(user)
	if has {
		c.Data["json"] = GetErrorMessage("User Exist")
		c.ServeJson()
		return
	}

	user.Attribute = attribute
	user.Command = command
	user.CreateTime = time.Now().Unix()

	// 在face++上创建用户
	personId, err := facesdk.CreatePerson(user.Attribute, personName)
	if err != nil {
		log.Panicln(err)
		c.Data["json"] = GetErrorMessage("Create User Failed")
		c.ServeJson()
		return
	}
	user.FacePersonId = personId

	// 添加到数据库
	n, err := models.Engine.Insert(user)
	if err != nil {
		c.Data["json"] = GetErrorMessage("Create User Failed")
		log.Panicln(err)
		c.ServeJson()
		return
	}

	c.Data["json"] = GetSuccessData(n)
	c.ServeJson()

	return
}

type UserUpdateController struct {
	beego.Controller
}

func (c *UserUpdateController) Get() {
	command := c.GetString("Command")
	if command == "" {
		c.Data["json"] = GetSuccessData("ok")
		c.ServeJson()
		return
	}

	personId, err := c.GetInt64("PersonId")
	if personId == 0 || err != nil {
		c.Data["json"] = GetErrorMessage("InvaideParams")
		c.ServeJson()
		return
	}

	user := new(models.Person)
	user.Command = command

	// 修改数据库
	_, err = models.Engine.Where("person_id = ?", personId).Cols("command").Update(user)
	if err != nil {
		c.Data["json"] = GetErrorMessage("UpdateFailed")
		c.ServeJson()
		return
	}

	c.Data["json"] = GetSuccessData("ok")
	c.ServeJson()
	return

}

type UserDeleteController struct {
	beego.Controller
}

func (c *UserDeleteController) Get() {
	personId, err := c.GetInt64("PersonId")
	if personId == 0 || err != nil {
		c.Data["json"] = GetErrorMessage("InvaideParams")
		c.ServeJson()
		return
	}

	user := new(models.Person)
	user.PersonId = personId

	has, _ := models.Engine.Get(user)
	if !has {
		c.Data["json"] = GetErrorMessage("UserNotExist")
		c.ServeJson()
		return
	}

	// 删除远程数据
	err = facesdk.DeletePerson(user.PersonName)
	if err != nil {
		c.Data["json"] = GetErrorMessage("DeleteUserFailed")
		c.ServeJson()
		return
	}

	// 删除本地数据
	_, err = models.Engine.Delete(user)
	if err != nil {
		c.Data["json"] = GetErrorMessage("UpdateFailed")
		c.ServeJson()
		return
	}

	c.Data["json"] = GetSuccessData("ok")
	c.ServeJson()
	return

}

type UserAddFaceController struct {
	beego.Controller
}

func (c *UserAddFaceController) Get() {

	// faceId
	faceId, err := c.GetInt64("FaceId")
	if faceId == 0 || err != nil {
		c.Data["json"] = GetErrorMessage("Need FaceId")
		c.ServeJson()
		return
	}

	// 需要添加到的用户id
	personId, err := c.GetInt64("PersonId")
	if personId == 0 || err != nil {
		c.Data["json"] = GetErrorMessage("Requited PersonId")
		c.ServeJson()
		return
	}

	user := new(models.Person)
	user.PersonId = personId

	has, _ := models.Engine.Get(user)
	if !has {
		c.Data["json"] = GetErrorMessage("UserNotExist")
		c.ServeJson()
		return
	}

	face := new(models.Face)
	face.FaceId = faceId

	has, _ = models.Engine.Get(face)
	if !has {
		c.Data["json"] = GetErrorMessage("FaceNotExist")
		c.ServeJson()
		return
	}

	// 添加用户到
	err = facesdk.AddFace(user.PersonName, face.FaceAddId)
	if err != nil {
		c.Data["json"] = GetErrorMessage("AddFaceFailed")
		c.ServeJson()
		return
	}

	// 训练
	facesdk.TrainVerify(user.PersonName)

	face.PersonId = personId

	// 修改本地数据库
	models.Engine.Where("face_id = ?", faceId).Cols("person_id").Update(face)

	c.Data["json"] = GetSuccessData("success")
	c.ServeJson()
	return
}

type UserAddStrangerController struct {
	beego.Controller
}

func (c *UserAddStrangerController) Get() {
	personId, err := c.GetInt64("StrangerId")
	if personId == 0 || err != nil {
		c.Data["json"] = GetErrorMessage("InvaideParams")
		c.ServeJson()
		return
	}

	user := new(models.Person)
	user.PersonId = personId

	has, _ := models.Engine.Get(user)
	if !has {
		c.Data["json"] = GetErrorMessage("UserNotExist")
		c.ServeJson()
		return
	}

	user.Attribute = "user"

	models.Engine.Where("person_id = ?", personId).Cols("attribute").Update(user)

	c.Data["json"] = GetSuccessData("ok")
	c.ServeJson()
	return

}

type UserDetailController struct {
	beego.Controller
}

func (c *UserDetailController) Get() {
	personId, err := c.GetInt64("StrangerId")
	if personId == 0 || err != nil {
		c.Data["json"] = GetErrorMessage("InvaideParams")
		c.ServeJson()
		return
	}

	user := new(models.Person)
	user.PersonId = personId

	has, _ := models.Engine.Get(user)
	if !has {
		c.Data["json"] = GetErrorMessage("UserNotExist")
		c.ServeJson()
		return
	}

	c.Data["json"] = GetSuccessData(user)
	c.ServeJson()
	return

}

// 判断用户与照片是否是同一个人
type UserVerifyController struct {
	beego.Controller
}

func (c *UserVerifyController) Get() {
	// faceId
	faceId, err := c.GetInt64("FaceId")
	if faceId == 0 || err != nil {
		c.Data["json"] = GetErrorMessage("Need FaceId")
		c.ServeJson()
		return
	}

	// 需要添加到的用户id
	personId, err := c.GetInt64("PersonId")
	if personId == 0 || err != nil {
		c.Data["json"] = GetErrorMessage("Requited PersonId")
		c.ServeJson()
		return
	}

	user := new(models.Person)
	user.PersonId = personId

	has, _ := models.Engine.Get(user)
	if !has {
		c.Data["json"] = GetErrorMessage("UserNotExist")
		c.ServeJson()
		return
	}

	face := new(models.Face)
	face.FaceId = faceId

	has, _ = models.Engine.Get(face)
	if !has {
		c.Data["json"] = GetErrorMessage("FaceNotExist")
		c.ServeJson()
		return
	}

	isSame, err := facesdk.RecVerify(user.PersonName, face.FaceAddId)
	if err != nil {
		log.Println(err)
		c.Data["json"] = GetErrorMessage("verify Failed")
		c.ServeJson()
		return
	}

	c.Data["json"] = GetSuccessData(isSame)
	c.ServeJson()
	return
}
